home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4STDZE.CPP < prev    next >
C/C++ Source or Header  |  1993-08-22  |  15KB  |  503 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4stdze.cpp
  5. //   Title:    ZIP+4 Engine
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class Z4_STANDARDIZE.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <z4.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //   Description:    Default constructor
  45. //    Parameters:
  46. //       Returns:    
  47. //----------------------------------------------------------------------------
  48. FN_M Z4_STANDARDIZE::Z4_STANDARDIZE()
  49. {
  50.     Z4_STANDARDIZE::Initialize(CL_INIT_CLASS);
  51. }
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Destructor
  56. //    Parameters:
  57. //       Returns:    
  58. //----------------------------------------------------------------------------
  59. FN_M Z4_STANDARDIZE::~Z4_STANDARDIZE()
  60. {
  61.     Z4_STANDARDIZE::Destroy(FALSE);
  62. }
  63.  
  64.  
  65. //----------------------------------------------------------------------------
  66. //   Description:    
  67. //    Parameters:    
  68. //       Returns:    TRUE if successful.
  69. //----------------------------------------------------------------------------
  70. VOID FN_M Z4_STANDARDIZE::Addr1Display(PSZ psz, RZ4_Z4 rz4)
  71. {
  72.     strcpy(psz, rz4.szSecName);
  73.     return ;
  74. }
  75.  
  76.  
  77. //----------------------------------------------------------------------------
  78. //   Description:    
  79. //    Parameters:    
  80. //       Returns:    TRUE if successful.
  81. //----------------------------------------------------------------------------
  82. VOID FN_M Z4_STANDARDIZE::Addr2Display(PSZ psz, RZ4_Z4 rz4, PCSZ pcszPriNo, PCSZ pcszSecNo, Z4_UNIT unit)
  83. {
  84.     CHAR szRange[MAX_RANGE+2];
  85.     if (pcszPriNo == NULL)
  86.         {
  87.         if (strcmp(rz4.szPriLo, rz4.szPriHi) == 0)
  88.             pcszPriNo = rz4.szPriLo;
  89.         else
  90.             pcszPriNo = "";
  91.         }
  92.     switch (rz4.rectype)
  93.         {
  94.        case Z4_TYPE_PM:
  95.        case Z4_TYPE_GD:
  96.             strcpy(psz, rz4.szPriName);
  97.             return ;
  98.  
  99.         case Z4_TYPE_POB:
  100.             strcpy(psz, rz4.szPriName);
  101.             if (pcszPriNo[0])
  102.                 strcats(psz, RangeDisplay(szRange, pcszPriNo));
  103.             return ;
  104.  
  105.         case Z4_TYPE_RR:
  106.             strcpy(psz, rz4.szPriName);
  107.             if (pcszPriNo[0])
  108.                 {
  109.                 strcats(psz, "BOX");
  110.                 strcats(psz, RangeDisplay(szRange, pcszPriNo));
  111.                 }
  112.             return ;             
  113.         }
  114.     strcpy(psz, RangeDisplay(szRange, pcszPriNo));
  115.     strcats(psz, Z4Directional(rz4.predir, Z4_DATA_ABBREV));
  116.     strcats(psz, rz4.szPriName);
  117.     if (rz4.fOrdinal)
  118.         strcat(psz, strord(atol(rz4.szPriName)));
  119.     if (rz4.suffix2)
  120.         strcats(psz, Z4SuffixFull(rz4.suffix2));
  121.     strcats(psz, Z4SuffixAbbrev(rz4.suffix1));
  122.     strcats(psz, Z4Directional(rz4.postdir, Z4_DATA_ABBREV));
  123.  
  124.     if (pcszSecNo == NULL)
  125.         {
  126.         if (strcmp(rz4.szSecLo, rz4.szSecHi) == 0)
  127.             pcszSecNo = rz4.szSecLo;
  128.         else
  129.             pcszSecNo = "";
  130.         }
  131.     if (pcszSecNo[0])
  132.         {
  133.         //
  134.         //    A parsed exceptional unit designator has the exceptional
  135.         //    designator in both the unit and secondary number field.
  136.         //    Be careful when peicing it back together.
  137.         //
  138.         if (strcmp(pcszSecNo, Z4Unit(unit, Z4_DATA_ABBREV)) != 0
  139.         || unit < Z4_UNIT_EXCEPT)
  140.             {
  141.            if (rz4.unit == Z4_UNIT_BLANK)
  142.                 {
  143.                 if (unit != Z4_UNIT_BLANK)
  144.                    strcats(psz, Z4Unit(unit, Z4_DATA_ABBREV));
  145.                 else
  146.                    strcats(psz, "#");
  147.  
  148.                 }
  149.            else
  150.                strcats(psz, Z4Unit(rz4.unit, Z4_DATA_ABBREV));
  151.             }
  152.         strcats(psz, RangeDisplay(szRange, pcszSecNo));
  153.         }
  154.     else if (unit != Z4_UNIT_BLANK && unit >= Z4_UNIT_EXCEPT)
  155.         strcats(psz, Z4Unit(unit, Z4_DATA_ABBREV));
  156.  
  157.     return ;
  158. }
  159.  
  160.  
  161. //----------------------------------------------------------------------------
  162. //   Description:    
  163. //    Parameters:    
  164. //       Returns:    
  165. //----------------------------------------------------------------------------
  166. VOID FN_M Z4_STANDARDIZE::Clear()
  167. {
  168.     szAddress[0] = '\0';
  169.     szAddr1[0] = '\0';
  170.     szAddr2[0] = '\0';
  171.     szLastLine[0] = '\0';
  172.                  
  173.     szCity[0] = '\0';
  174.     state = Z4_ST_INVALID;
  175.     szZip5[0] = '\0';
  176.     szAddon[0] = '\0';
  177.     szZip4[0] = '\0';
  178.  
  179.     fFacilityOther = FALSE;
  180.     fNonDeliverable = FALSE;
  181.     return ;
  182. }
  183.  
  184.  
  185. //----------------------------------------------------------------------------
  186. //   Description:    
  187. //    Parameters:
  188. //       Returns:    TRUE if successful.
  189. //----------------------------------------------------------------------------
  190. BOOL FN_M Z4_STANDARDIZE::Complete()
  191. {
  192.     szAddress[0] = '\0';
  193.     
  194.     if (szAddr1[0])
  195.         strcpy(szAddress, szAddr1);
  196.     if (szAddr2[0])
  197.         {
  198.         if (szAddress[0])
  199.             strcat(szAddress, "\r\n");
  200.         strcat(szAddress, szAddr2);
  201.         }
  202.     if (szLastLine[0])
  203.         {
  204.         if (szAddress[0])
  205.             strcat(szAddress, "\r\n");
  206.         strcat(szAddress, szLastLine);
  207.         }
  208.     return TRUE;
  209. }
  210.  
  211.  
  212. //----------------------------------------------------------------------------
  213. //   Description:    
  214. //    Parameters:
  215. //       Returns:    TRUE if successful.
  216. //----------------------------------------------------------------------------
  217. BOOL FN_M Z4_STANDARDIZE::CS(RZ4_CX rcx, PCSZ pcszZip5)
  218. {
  219.     if (!Z4_INQ::z5_file.Find(Z4_INQ::z5, pcszZip5))
  220.         return FALSE;
  221.                                                     // Find actual city/state file record!
  222.     for (SIZET i = 0; Z4_INQ::z5.cCities; ++i)
  223.         {
  224.         if (!Z4_INQ::cs_file.Record(Z4_INQ::cs, Z4_INQ::z5.arecid[i]))
  225.             return FALSE;
  226.  
  227.         if (Z4_INQ::cs.state == rcx.state
  228.         && strcmp(Z4_INQ::cs.szCity, rcx.szCity) == 0)
  229.             break;
  230.         }
  231.     fFacilityOther = Z4_INQ::cs.facility == Z4_FACILITY_OTHER;
  232.     if (!LastLine(Z4_INQ::cs, pcszZip5))
  233.         return FALSE;
  234.     return Complete();
  235. }
  236.  
  237.  
  238. //----------------------------------------------------------------------------
  239. //   Description:    Destroy object. Free any resources used by object.
  240. //                          Normally called by destructor.
  241. //                        Should allow multiple calls from various classes.
  242. //                        A class should almost always re-init its variables when 
  243. //                        it is destroyed to prevent accidents.
  244. //    Parameters:    fDestroyAll        Destroy parents also?
  245. //                                            Default is TRUE.
  246. //       Returns:    TRUE if successful.
  247. //----------------------------------------------------------------------------
  248. BOOL FN_M Z4_STANDARDIZE::Destroy(BOOL fDestroyAll)
  249. {
  250.     Z4_STANDARDIZE::Initialize(CL_INIT_CLASS_VARS);
  251.     if (fDestroyAll)                            // Destroy parent.
  252.         Z4_STANDARDIZE_PARENT::Destroy(fDestroyAll);
  253.     return TRUE;
  254. }
  255.  
  256.  
  257. //----------------------------------------------------------------------------
  258. //   Description:    Initialize object. 
  259. //                          Normally called by constructor.
  260. //                        Should allow multiple calls from various classes.
  261. //    Parameters:    sInit        Initialization code. May be one of the following:
  262. //                                        CL_INIT_CLASS            Reset class variables and
  263. //                                                                    and dynamic allocations for
  264. //                                                                    this class only.
  265. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  266. //                                                                    this class only.
  267. //                                        CL_INIT_VARS            Reset class variables for
  268. //                                                                    this class only.
  269. //                                        CL_INIT_ALL                Initialize class and all 
  270. //                                                                    parent class, including
  271. //                                                                    dynamic memory allocation.
  272. //                                    Default is CL_INIT_ALL
  273. //       Returns:    TRUE if successful.
  274. //----------------------------------------------------------------------------
  275. BOOL FN_M Z4_STANDARDIZE::Initialize(SHORT sInit)
  276. {
  277.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  278.         Z4_STANDARDIZE_PARENT::Initialize(sInit);
  279.  
  280.     return TRUE;
  281. }
  282.  
  283.  
  284. //----------------------------------------------------------------------------
  285. //   Description:
  286. //    Parameters:
  287. //       Returns:    TRUE if successful.
  288. //----------------------------------------------------------------------------
  289. BOOL FN_M Z4_STANDARDIZE::LastLine(RZ4_CS rcs, PCSZ pcszZip5, PCSZ pcszAddon)
  290. {
  291.     Assert(pcszZip5);
  292.  
  293.     state = rcs.state;
  294.     strcpy(szZip5, pcszZip5);
  295.  
  296.     fFacilityOther = rcs.facility == Z4_FACILITY_OTHER;
  297.     strcpy(szCity, (fFacilityOther ? rcs.szLastLineName: rcs.szCity));
  298.     if (pcszAddon && pcszAddon[0] != ' ' && pcszAddon[0] != 'N')
  299.         {
  300.         strcpy(szZip4, pcszZip5);
  301.         strcat(szZip4, "-");
  302.         strcat(szZip4, pcszAddon);
  303.         strcpy(szAddon, pcszAddon);
  304.         }
  305.     else
  306.         szAddon[0] = '\0';
  307.     LastLineDisplay(szLastLine, rcs, pcszZip5, pcszAddon);
  308.     return TRUE;
  309. }
  310.  
  311.  
  312. //----------------------------------------------------------------------------
  313. //   Description:    
  314. //    Parameters:    
  315. //       Returns:    TRUE if successful.
  316. //----------------------------------------------------------------------------
  317. VOID FN_M Z4_STANDARDIZE::LastLineDisplay(PSZ psz, RZ4_CS rcs, PCSZ pcszZip5, PCSZ pcszAddon)
  318. {
  319.     BOOL fFacilityOther = rcs.facility == Z4_FACILITY_OTHER;
  320.     PCSZ pcszCity = (fFacilityOther ? rcs.szLastLineName: rcs.szCity);
  321.     sprintf(psz, "%s %s %s", pcszCity, Z4_ST_FILE::Abbreviation(rcs.state), pcszZip5);
  322.     if (pcszAddon && pcszAddon[0] != ' ' && pcszAddon[0] != 'N')
  323.         {
  324.         strcat(psz, "-");
  325.         strcat(psz, pcszAddon);
  326.         }
  327.     return ;
  328. }
  329.  
  330.  
  331. //----------------------------------------------------------------------------
  332. //   Description:
  333. //    Parameters:
  334. //       Returns:    TRUE if successful.
  335. //----------------------------------------------------------------------------
  336. VOID FN_M Z4_STANDARDIZE::PriRangeDisplay(PSZ psz, RZ4_Z4 rz4)
  337. {
  338.     CHAR szRange[MAX_RANGE+2];
  339.     if (rz4.szPriLo[0])
  340.         {
  341.         strcpy(psz, RangeDisplay(szRange, rz4.szPriLo));
  342.         if (rz4.szPriHi[0] && strcmp(rz4.szPriLo, rz4.szPriHi) != 0)
  343.             {
  344.             strcats(psz, "-");
  345.             strcats(psz, RangeDisplay(szRange, rz4.szPriHi));
  346.             }
  347.         }
  348.     else
  349.         psz[0] = '\0';
  350.     return ;
  351. }
  352.  
  353.  
  354. //----------------------------------------------------------------------------
  355. //   Description:    
  356. //    Parameters:    
  357. //       Returns:    TRUE if successful.
  358. //----------------------------------------------------------------------------
  359. PSZ FN_M Z4_STANDARDIZE::RangeDisplay(PSZ psz, PCSZ pcszRange)
  360. {
  361.     PSZ pszSlash;
  362.  
  363.     strcpy(psz, pcszRange);
  364.     if (psz[0] && (pszSlash = strchr(psz + 1, '/')) != NULL)
  365.         {
  366.         pszSlash--;
  367.         pszSlash[0] = ' ';
  368.         pszSlash++;
  369.         strcpy(pszSlash, strchr(pcszRange + 1, '/') - 1);
  370.         }
  371.     return psz;
  372. }
  373.  
  374.  
  375. //----------------------------------------------------------------------------
  376. //   Description:    
  377. //    Parameters:    
  378. //       Returns:    TRUE if successful.
  379. //----------------------------------------------------------------------------
  380. VOID FN_M Z4_STANDARDIZE::SecRangeDisplay(PSZ psz, RZ4_Z4 rz4)
  381. {
  382.  
  383.     CHAR szRange[MAX_RANGE+2];
  384.  
  385.     if (rz4.szSecLo[0])
  386.         {
  387.         if (rz4.unit == Z4_UNIT_BLANK)
  388.             strcpy(psz, "#");
  389.         else
  390.             strcpy(psz, Z4Unit(rz4.unit, Z4_DATA_ABBREV));
  391.         strcats(psz, RangeDisplay(szRange, rz4.szSecLo));
  392.         if (rz4.szSecHi[0] && strcmp(rz4.szSecLo, rz4.szSecHi) != 0)
  393.             {
  394.             strcats(psz, "-");
  395.             
  396.             strcats(psz, RangeDisplay(szRange, rz4.szSecHi));
  397.             }
  398.         }
  399.     else if (rz4.unit >= Z4_UNIT_EXCEPT)
  400.         sprintf(psz, "%s", Z4Unit(rz4.unit, Z4_DATA_ABBREV));
  401.     else if (rz4.unit != Z4_UNIT_BLANK)
  402.         sprintf(psz, "(%s)", Z4Unit(rz4.unit, Z4_DATA_ABBREV));
  403.     else
  404.         psz[0] = '\0';
  405.     return ;
  406. }
  407.  
  408.  
  409. //----------------------------------------------------------------------------
  410. //   Description:    
  411. //    Parameters:
  412. //       Returns:    TRUE if successful.
  413. //----------------------------------------------------------------------------
  414. BOOL FN_M Z4_STANDARDIZE::Z4(RRECID rrecid, RZ4_PARSE rparse)
  415. {
  416.     if (!Z4_INQ::z4_file.Record(Z4_INQ::z4, rrecid))
  417.         return FALSE;
  418.  
  419.     if (!Z4_INQ::z5_file.Find(Z4_INQ::z5, Z4_INQ::z4.szZip5))
  420.         return FALSE;
  421.  
  422.     if (!Z4_INQ::cs_file.Best(Z4_INQ::cs, Z4_INQ::z5, Z4_INQ::z4.szLastLine))
  423.         return FALSE;
  424.  
  425.     // 
  426.     // This is a problem. I need to specify the correct addon here!!
  427.     //    POB and FIRM can be ranged??
  428.     //
  429.     PCSZ pcszAddon = NULL;
  430.     if ((Z4_INQ::z4.szPriLo[0] && rparse.szPriNoReformat[0])
  431.     || (!Z4_INQ::z4.szPriLo[0] && !rparse.szPriNoReformat[0]))
  432.         {
  433.         pcszAddon = Z4_INQ::z4.szAddonLo;
  434.         if (Z4_INQ::z4.rectype == Z4_TYPE_POB
  435.         && strcmp(Z4_INQ::z4.szAddonLo, Z4_INQ::z4.szAddonHi) != 0)
  436.             {
  437.             LONG lAddonHi = atol(Z4_INQ::z4.szAddonHi);
  438.             LONG lAddonLo = atol(Z4_INQ::z4.szAddonLo);
  439.             LONG lPriLo = atol(Z4_INQ::z4.szPriLo);
  440.             LONG lPri = atol(rparse.szPriNoReformat);
  441.             LONG lAddon = lAddonLo + (lPri - lPriLo);
  442.  
  443.             if (lAddon >= lAddonLo && lAddon <= lAddonHi)
  444.                 {
  445.                 sprintf(szAddon, "%04ld", lAddon);
  446.                 pcszAddon = szAddon;
  447.                 }
  448.             }
  449.         }
  450.     if (!LastLine(Z4_INQ::cs, Z4_INQ::z4.szZip5, pcszAddon))
  451.         return FALSE;
  452.  
  453.     strcpy(szAddr1, rparse.szSecName);
  454.     Addr2Display(szAddr2, Z4_INQ::z4, rparse.szPriNoReformat, rparse.szSecNo, rparse.unit);
  455.     fNonDeliverable = (szAddon[0] == 'N' || szAddon[0] == ' ' || !szAddon[0]);
  456.     return Complete();
  457. }
  458.  
  459.  
  460. //----------------------------------------------------------------------------
  461. //   Description:    
  462. //    Parameters:    
  463. //       Returns:    TRUE if successful.
  464. //----------------------------------------------------------------------------
  465. BOOL FN_M Z4_STANDARDIZE::Z5(RRECID rrecid, PCSZ pcszZip5)
  466. {
  467.     if (!Z4_INQ::cs_file.Record(Z4_INQ::cs, rrecid))
  468.         return FALSE;
  469.  
  470.     if (!LastLine(Z4_INQ::cs, pcszZip5))
  471.         return FALSE;
  472.  
  473.     return Complete();
  474. }
  475.  
  476.  
  477. //----------------------------------------------------------------------------
  478. //   Description:    
  479. //    Parameters:
  480. //       Returns:    TRUE if successful.
  481. //----------------------------------------------------------------------------
  482. BOOL FN_M Z4_STANDARDIZE::ZX(RRECID rrecid, PCSZ pcszAddon)
  483. {
  484.     if (!Z4_INQ::z4_file.Record(Z4_INQ::z4, rrecid))
  485.         return FALSE;
  486.  
  487.     if (!Z4_INQ::z5_file.Find(Z4_INQ::z5, Z4_INQ::z4.szZip5))
  488.         return FALSE;
  489.  
  490.     if (!Z4_INQ::cs_file.Best(Z4_INQ::cs, Z4_INQ::z5, Z4_INQ::z4.szLastLine))
  491.         return FALSE;
  492.  
  493.     if (!LastLine(Z4_INQ::cs, Z4_INQ::z4.szZip5, pcszAddon))
  494.         return FALSE;
  495.  
  496.     Addr1Display(szAddr1, Z4_INQ::z4);
  497.     Addr2Display(szAddr2, Z4_INQ::z4);
  498.     return Complete();
  499. }
  500. //----------------------------------------------------------------------------
  501. //------------------------------- End of File --------------------------------
  502. //----------------------------------------------------------------------------
  503.